package com.kaiwukj.android.ufamily.mvp.ui.page.service.detail;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.app.constant.Constant;
import com.kaiwukj.android.ufamily.di.component.DaggerServiceComponent;
import com.kaiwukj.android.ufamily.di.module.ServiceModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ServiceResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.home.message.ChatActivity;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.ServiceContract;
import com.kaiwukj.android.ufamily.mvp.ui.widget.home.ShopWebView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.ServiceDetailUrl)
public class ServiceDetailActivity extends BaseMvpActivity<ServiceDetailPresenter> implements ServiceContract.DetailView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.web)
    ShopWebView webView;
    @BindView(R.id.container_bottom)
    ViewGroup containerBottom;

    @Autowired
    int serviceId;
    ServiceResult service;
    @Autowired
    int serviceType = 0;//0:normal other:GM

    private boolean isLoadFinish = false;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @OnClick(R.id.tv_manager)
    void onManagerClick() {
        // showMessage("管家");
        String keeperHxName = getApp().getUserInfo().getKeeperHxName();
        if (!StringUtils.isEmpty(keeperHxName)) {
            ChatActivity.startKeeperChat(this, keeperHxName);
        } else {
            ARouter.getInstance().build(ARouterUrlKt.MineKeeperContainerUrl).navigation();
            // showMessage("管家信息获取失败");
        }
    }

    @OnClick(R.id.tv_go)
    void onServiceGo() {
        //showMessage("预约");
        if (service == null) {
            showMessage("服务获取失败");
            return;
        }
        ServiceAppointmentDialog dialog = new ServiceAppointmentDialog(this, service);
        dialog.show();
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerServiceComponent.builder()
                .appComponent(appComponent)
                .serviceModule(new ServiceModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_service_detail;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        switch (serviceType) {
            case 0:
                tvTitle.setText("服务详情");
                break;
            case 1:
                tvTitle.setText("新房装修");
                break;
            case 2:
                tvTitle.setText("旧房改造");
                break;
            case 3:
                tvTitle.setText("免费报价");
                break;
            case 4:
                tvTitle.setText("免费设计");
                break;
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {


        //String url = "http://172.168.1.71:8899/serviceDetails.html?serviceId=12&isApp=1";
        String loadUrl = "";
        if (serviceType == 0) {
            //    String[] images = service.getCarImg().split(",");
            loadUrl = String.format("%s?serviceId=%s&isApp=1", Constant.isDev ? Constant.SERVICE_DETAIL_HOST_TEST : Constant.SERVICE_DETAIL_HOST, serviceId);
        } else {
            loadUrl = String.format("%s/index.html", Constant.isDev ? Constant.GM_H5_HOST_TEST : Constant.GM_H5_HOST);
            //loadUrl = "http://jzh5test.jiayuanli.net/index.html";
            containerBottom.setVisibility(View.GONE);
        }
        // loadUrl = "http://192.168.188.33:8890/";
        webView.setOnServiceLiner(new ShopWebView.OnServiceListener() {
            @Override
            public void onTokenInvalid() {

            }

            @Override
            public void onGoHome() {
                onBackClick();
            }
        });

        webView.loadUrl(loadUrl);
        webView.addOnPageStateListener(new ShopWebView.onPageStateListener() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                showLoading();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                if (isLoadFinish) {
                    hideLoading();
                    isLoadFinish = false;
                } else {
                    isLoadFinish = true;
                }
                if (serviceType != 0) {
                    hideLoading();
                    webView.setServiceInfo(serviceType);
                }
            }
        });

        if (serviceType == 0) {
            //mPresenter.getService(serviceId);
            //mPresenter.fetchParams(serviceId);
            mPresenter.fetchData(serviceId);
        }

    }


    @Override
    public void onFetchServiceParams(List<ServiceResult.ServiceInfo> result) {
        service.setServiceParamList(result);


    }

    @Override
    public void onGetService(ServiceResult result) {
        service = result;
        if (isLoadFinish) {
            hideLoading();
            isLoadFinish = false;
        } else {
            isLoadFinish = true;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
